<?php
namespace app\components\pay;
use Yii;
use app\components\Func;

/*支付抽象类*/
abstract class Pay
{
    protected $appid;
    protected $paymentObj;
    protected $acceptPaymentObj;
    protected $partner_key;
    
    protected $packages = array();
    abstract public function prepay(array $paras);//预支付
    abstract public function micropay(array $paras);//刷卡支付（被扫支付）
    abstract public function orderquery(array $paras);//查询定单
    abstract public function refund(array $paras);//退款
    abstract public function refundquery(array $paras);//退款查询
    abstract public function cancel(array $paras);//定单撤销
    abstract public function getSign($paras);//生成签名
    abstract public function checkNotifySign($paras);//检查支付通知的签名
    
    public function __construct($paymentObj, $appid = '', $acceptPaymentObj = '') {
        $this->paymentObj = $paymentObj;
        $this->acceptPaymentObj = $acceptPaymentObj;
        $this->partner_key = empty($acceptPaymentObj) ? $paymentObj->partner_key : $acceptPaymentObj->apikey;
        $this->appid = $appid;
    }
    //获取支付成功通知url
    public function notifyurl() {
        return Yii::$app->urlManager->createAbsoluteUrl('pay/wxpaynotify');
    }

    public static function getResBuyJavaApi($api, $params) {
        $javaJson = Yii::$app->kelaileJava->getJavaRes($api, $params);
        if(empty($javaJson)) throw new \Exception('Java接口超时:'.$api,2022029);
        $javaRes = json_decode($javaJson, true);
        if(empty($javaRes)) throw new \Exception('Java接口返回空',2022035);
        if(!isset($javaRes['code'])) throw new \Exception('Java:'.$javaJson);
        if($javaRes['code'] == 4292) throw new \Exception("Java:商家账户余额不足",2022046);
        if($javaRes['code'] != 0) throw new \Exception($javaRes['msg']?:$javaJson, $javaRes['code']);
        return $javaRes;
    }
    
}
